'use strict';
const { think } = require('thinkjs');
// +----------------------------------------------------------------------
// | CmPage [ 通用页面框架 ]
// +----------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0
// +----------------------------------------------------------------------
// | Author: defans <defans@sina.cn>
// +----------------------------------------------------------------------

/**
 @module cmpage.service
 */

/**
 * 实现了文件列表页面的功能，定制了移动端的页面
 * @class cmpage.service.file_list
 */
const CMPage = require('../cmpage/page_mob.js');

module.exports = class extends CMPage {


     /**
     * 重写父类的 pageEditInit 方法，对初始化编辑页面的值进行修改
     * @method  pageEditInit
     * @return {string}  where条件子句
     */
    async pageEditInit() {
        let md = await super.pageEditInit();

        this.cmpage.debug(this.mod.parmsUrl,'file_list--mod.parmsUrl');

        if(md.id ==0){
            md.c_type = this.mod.parmsUrl.c_type;
            md.c_link_type = this.mod.parmsUrl.c_link_type;
            md.c_link = this.mod.parmsUrl.c_link;
        }

        //console.log(md);
        return md;
    }

    async getDataViewRec(){
        let md = await super.getDataViewRec();
        if(!think.isEmpty(md)){
            if(!this.cmpage.isImageFile(md.c_path)){
                md.c_path = `<a href='${md.c_path}' >点击下载</a>`;
                for (let col of this.modCols) {
                    if(col.c_column == 'c_path'){
                        col.c_type ='text';
                    }
                }
            }
        }
        return md;
    }

    async pageSave(parms){
        if(think.isEmpty(parms)){
            return {
                statusCode: 500,
                message: '保存失败！'
            };
        }
        //this.cmpage.debug(parms,'xxxxxxxxxxxx');
        if(think.isEmpty(parms.c_path)){
            let oldRecord = JSON.parse(parms.old_record);
            //this.cmpage.debug(oldRecord,'OLDRECORD');
            parms.c_path = oldRecord.c_path;
            return await super.pageSave(parms);
        }else{
            if(think.isArray(parms.c_path)){
                let files = parms.c_path;       //多个文件        
                for(let i=0; i<files.length; i++){
                    parms.c_path = files[i];
                    let fileName = files[i].split('/');
                    parms.c_name = fileName[fileName.length -1];
                    parms.c_size = parms.fileSize[i] / 1000;
                    await super.pageSave(parms);
                }    
            }else{
                let fileName = parms.c_path.split('/');
                parms.c_name = fileName[fileName.length -1];
                parms.c_size = parms.fileSize / 1000;
                await super.pageSave(parms);
            }
            return {
                statusCode: 200,
                message: '保存成功！'
            };    
        }
    }

    /**
     * 取模块列表中的MUI设置，组合成HTML输出，一般在子类中通过重写这个方法来达到页面定制的效果
     * @method  mobHtmlGetList
     * @return  {string}  HTML片段
     */
    async mobHtmlGetList() {
        let html = [];

        this.mobGetPageMuiSetting();
        await this.getDataList();
        html.push('<div class="mui-content-padded">');
        for (let row of this.list.data) {
            //处理替换值
            for (let col of this.modCols) {
                if (col.c_type === "replace" && !(/^select/.test(col.c_memo))) {
                    row[col.c_column] = await this.getReplaceText(row[col.c_column], col.c_memo);
                } else if (col.c_coltype === 'timestamp') {
                    row[col.c_column] = think.datetime(row[col.c_column]);
                }
                if (col.c_column === 'c_path') {
                    row[col.c_column] = '#domainName#' + row[col.c_column];
                }
            }
            html.push(`<p>${row.c_name}</p>`);
            if (!think.isEmpty(row.c_memo)) html.push(`<p>${row.c_memo}</p>`);
            if (cmpage.isImageFile(row.c_path)) {
                html.push(`<p><img style="width: 100%;" src="${row.c_path}" data-preview-src="" data-preview-group="1" /></p>`);
            } else {
                html.push(`<p><a href="${row.c_path}" />下载请至电脑端</a></p>`)
            }
        }

        return html.join(' ');
    }

    async mobHtmlGetHeaderBtns() {
        //不用输出头部按钮，手机端自己设置
        return ['', '']
    }

}